package com.whu.UVAProtocol;

import com.whu.util.ByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @Description
 * @Author chengeng
 * @Date 2021/10/28 17:03
 **/
@Slf4j
public class UVAMsgDecoder extends ReplayingDecoder<Void> {


    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {

        short magicNumber = byteBuf.readShort();
        byte addressProp = byteBuf.readByte();
        byte controlProp = byteBuf.readByte();
        byte[] divs = new byte[4];
        byteBuf.readBytes(divs);
        char serialNum = byteBuf.readChar();
        short msgBodyLen = byteBuf.readShort();
        byte[] msgBody = new byte[msgBodyLen-(8+8+4+1+5)];
        byteBuf.readBytes(msgBody);

        double longitude = byteBuf.readDouble();
        double latitude = byteBuf.readDouble();
        float height = byteBuf.readFloat();

        byte battery = byteBuf.readByte();
        byte[] timeStamp = new byte[5];
        byteBuf.readBytes(timeStamp);
        short crcCheckNum = byteBuf.readShort();


        log.info("解析来自无人机"+addressProp+"流水号为"+(int)serialNum +"的数据");
        log.info("消息体长度："+msgBody.length);
        UVAMessageProtocolType1_3 protocolType1_3 = new UVAMessageProtocolType1_3(
                magicNumber,  addressProp,  controlProp,
                divs,  msgBodyLen,  msgBody, serialNum, longitude,
                latitude,  height,  battery,  timeStamp,
                crcCheckNum);
        log.info(protocolType1_3.toString());
        list.add(protocolType1_3);
    }
}
